package com.bkgy.modular.tbprospeed.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.commom.model.Temp2;
import com.bkgy.modular.tbprospeed.dao.TbProSpeedDao;
import com.bkgy.modular.tbprospeed.entity.SpeedStatisticsResp;
import com.bkgy.modular.tbprospeed.entity.TbProSpeedData;
import com.bkgy.modular.tbprospeed.entity.TbProSpeedReq;
import com.bkgy.modular.tbprospeed.service.TbProSpeedService;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * Author:Lipeng Zhao
 * Description:
 *
 * @Date:Created In 10:19 2025/10/18
 */
@Service
public class TbProSpeedServiceImpl implements TbProSpeedService {

    @Autowired
    private TbProSpeedDao tbProSpeedDao;

    /**
     * 分页查看记录
     *
     * @param data 分页参数字符串
     */
    public ResultData findDataTbProSpeedData(String data) {
        try {
            TbProSpeedReq jtbPaopianPdiData = JSON.parseObject(data, TbProSpeedReq.class);
            Integer pageIndex = jtbPaopianPdiData.getPageIndex();
            Integer pageSize = jtbPaopianPdiData.getPageSize();

            if (null == pageIndex || null == pageSize) {
                return ResultData.ResultDataFaultSelf("分页参数没有传", null);
            }
            // 获取查询条件
            JSONObject jsonData = JSON.parseObject(jtbPaopianPdiData.getCondition().toString());
            Page<?> page = new Page<>(pageIndex, pageSize);
            List<TbProSpeedData> list = tbProSpeedDao.findDataTbProSpeedDataByPage(page, jsonData);
            Integer count = tbProSpeedDao.findDataTbProSpeedDataByPageSize(jsonData);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 按照钢种 组统计速度未达标率
     */
    public ResultData speedStatisticsByShift() {
        try {
            List<SpeedStatisticsResp> list = tbProSpeedDao.speedStatisticsByShift();
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }

    public ResultData speedStatisticsBySteelGrade() {
        try {
            List<SpeedStatisticsResp> list = tbProSpeedDao.speedStatisticsBySteelGrade();
            return ResultData.ResultDataSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }

    }

    @Override
    public ResultData reWeldRate(String beginDate, String steelGrade) {
        Date d1 = DateUtil.getStringToDate("yyyyMMdd", beginDate);
        Date d2 = DateUtil.getNextDayZero(d1);
        String endDate = DateUtil.getDateToString(d2, "yyyyMMdd");

        //查询昨日班组
        List<String> l = tbProSpeedDao.selectCrew(beginDate, endDate);

        List<Temp2> list = tbProSpeedDao.selectReWeldRate(beginDate, endDate, steelGrade);
        //分组
        Map<String, List<Temp2>> map = new HashMap<>();
        for(String str : l) {
            map.put(str, new ArrayList<>());
        }
        for (Temp2 t : list) {
            String crew = t.getK();
            if (!map.containsKey(crew)) {
                List<Temp2> tempList = new ArrayList<>();
                tempList.add(t);
                map.put(crew, tempList);
            } else {
                map.get(crew).add(t);
            }
        }
        JSONObject res = new JSONObject();
        List<String> crewList = new ArrayList<>();
        List<BigDecimal> rateList = new ArrayList<>();
        for (Map.Entry<String, List<Temp2>> entry : map.entrySet()) {
            String crew = entry.getKey();
            crewList.add(CommonConstant.crewMap.get(crew));
            int c = 0;
            List<Temp2> tempList = entry.getValue();
            for (Temp2 t : tempList) {
                if (t.getV() > 1) c++;
            }
            BigDecimal rate = BigDecimal.ZERO;
            if(tempList.size()>0){
                rate = BigDecimal.valueOf(c).multiply(BigDecimal.valueOf(100))
                        .divide(BigDecimal.valueOf(tempList.size()), 2, RoundingMode.HALF_UP);
            }
            rateList.add(rate);
        }
        res.put("crewList", crewList);
        res.put("rateList", rateList);
        return ResultData.ResultDataSuccess(res);
    }
}
